<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>Shoes On Line 后台管理系统</h2>
        <p>请登录您的账户</p>
      </div>
      
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
            size="large">
          </el-input>
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            size="large"
            @keyup.enter.native="handleLogin">
          </el-input>
        </el-form-item>
        
        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading"
            @click="handleLogin" 
            class="login-button"
            size="large">
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <!-- <div class="login-tips">
        <p>默认用户名：admin</p>
        <p>默认密码：123456</p>
      </div> -->
    </div>
  </div>
</template>

<script>
import axios from 'axios' // 导入axios用于网络请求

export default {
  name: 'LoginPage',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          
          try {
  
                         // 调用实际登录接口
             const response = await axios.get('/api/admin/login', {
               params: {
                 username: this.loginForm.username,
                 password: this.loginForm.password
               }
             })
            console.log(response.data);
            // 根据接口返回格式判断登录是否成功
            if (response.data.code === 200) {
              this.$message.success(response.data.msg || '登录成功！')
              
              // 存储登录状态
              localStorage.setItem('isLoggedIn', 'true')
              localStorage.setItem('username', this.loginForm.username)
              
              // 跳转到主页
              this.$router.push('/dashboard')
            } else {
              this.$message.error(response.data.msg || '登录失败，请检查用户名和密码')
            }
          } catch (error) {
            this.$message.error('网络错误或服务器异常，请稍后重试')
            console.error('登录请求失败:', error)
          } finally {
            this.loading = false
          }
        }
      })
    }
  },
  mounted() {
    // 如果已经登录，直接跳转到主页
    if (localStorage.getItem('isLoggedIn') === 'true') {
      this.$router.push('/dashboard')
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 400px;
  background: white;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  padding: 40px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: #303133;
  margin-bottom: 10px;
  font-size: 24px;
}

.login-header p {
  color: #909399;
  font-size: 14px;
}

.login-form {
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
}

.login-tips {
  text-align: center;
  color: #909399;
  font-size: 12px;
  line-height: 1.5;
}

.login-tips p {
  margin: 5px 0;
}
</style>